package com.prs.crm.service.crm.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.crm.CustomerDao;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.CustomerTrade;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class CustomerServiceImpl extends BaseServiceImpl<Customer,CustomerDao> implements CustomerService{

	@Override
	public void saveCustomer(Customer customer, File file, String fileName) {
		// TODO Auto-generated method stub
		
		customer.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(customer);
	}

	@Override
	public Collection<?> getCustomerTypeReport(Date beginTime, Date endTime) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerTypeReport(beginTime, endTime);
	}

	@Override
	public Collection<?> getCustomerBusinessReport(Date beginTime, Date endTime) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerBusinessReport(beginTime, endTime);
	}

	@Override
	public Collection<?> getReportSouce(Date dates, Person person, Integer type, String merit) {
		// TODO Auto-generated method stub
		return this.getDao().getReportSouce(dates, person, type, merit);
	}

	@Override
	public Collection<Customer> getAllCustomer() {
		// TODO Auto-generated method stub
		return this.getDao().getAllCustomer();
	}

	@Override
	public boolean existSameCustomer(String name) {
		// TODO Auto-generated method stub
		boolean flag =false;
		Collection<Customer> customers = this.getDao().getCustomerByName(name);
		if(customers.size() != 0) {
			flag = true;
		}
		return flag;
	}
	
	public Collection<Customer> getAllLiaisonCustomers()
	{
		return this.getDao().getAllLiaisonCustomers();
	}

	@Override
	public Collection<Customer> getAllNoneLiaisonCustomers(String tradeIds) {
		// TODO Auto-generated method stub
		return this.getDao().getAllNoneLiaisonCustomers(tradeIds);
	}

	@Override
	public Collection<Customer> getCustomerAddReportSouce(Date dates, Integer type) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerAddReportSouce(dates, type);
	}

	@Override
	public Collection<CustomerTrade> convertToDeptDocTrade(String ids) {
		// TODO Auto-generated method stub
		try {
			ArrayList<CustomerTrade> trades = new ArrayList<CustomerTrade>();
			for(String id : ids.split(",")) {
				CustomerTrade trade = new CustomerTrade();
				trade.setId(Integer.valueOf(id));
				trades.add(trade);
			}
			
			return trades;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public Collection<Customer> getCustomerByLay() {
		return 	this.getDao().getCustomerByLay();
		
	}

	@Override
	public boolean existSamecustomercode(String code) {
		// TODO Auto-generated method stub
		boolean flag =false;
		Collection<Customer> customers = this.getDao().getCustomerByName(code);
		if(customers.size() != 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public void saveCustomers(Customer customer, File file, String fileName) {
		// TODO Auto-generated method stub
		Attach cAttach=FileUtil.getInstance().saveFile(file, fileName,"",Constrains.UPLOAD_DIR_CUSTOMER);
		
		if(cAttach.getSrc()!=null){
			customer.setCustomer_file(cAttach.getSrc());
		}
		this.getDao().save(customer);
	}

	@Override
	public List<Customer> getCustomerByAgent(CustomerAgent customerAgent) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerByAgent(customerAgent);
	}

	@Override
	public List<Customer> getCustomerBySupplier(
			CustomerSupplier customerSupplier) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerBySupplier(customerSupplier);
	}


}
